﻿Public Class NhaDAO
    Private connect As Connect
    Private sqlstr As String
    Public Sub New()
        connect = New Connect
    End Sub
    Public Function CapNhat(ByVal emp As NhaDTO, ByVal flag As Boolean)
        Dim manha As String = emp.MaThongTinNha
        Dim maloainha As String = emp.MaLoaiNha
        Dim dientich As String = emp.DienTich
        Dim giathue As String = emp.GiaThue
        Dim matienich As String = emp.MaTienIch
        Dim tgthue As String = emp.ThoiGianThue
        Dim ngaydang As String = emp.NgayDang
        Dim songuoio As String = emp.SoNguoiO
        Dim MaNhanVien As String = emp.MaNhanVien
        Dim cmndchunha As String = emp.CMNDChuNha

        '-------------------------------------------'
        If flag = True Then
            sqlstr = "INSERT THONGTINNHA (MaThongTinNha, MaLoaiNha,DienTich, GiaThue, MaTienIch,ThoiGianThue,NgayDang,SoNguoiO,MaNhanVien,CMNDChuNha)" & _
                        " VALUES('" & manha & "', '" & maloainha & "', '" & _
                                        dientich & "', '" & giathue & "','" & matienich & "', '" & _
                                        tgthue & "', '" & ngaydang & "', '" & songuoio & "','" & _
                                        MaNhanVien & "', '" & cmndchunha & "' )"

        Else
            sqlstr = "UPDATE THONGTINNHA SET  MaThongTinNha  = '" & manha & _
                                        "', MaLoaiNha   = '" & maloainha & _
                                        "', DienTich      = '" & dientich & _
                                        "',  GiaThue      = '" & giathue & _
                                         "',  MaTienIch      = '" & matienich & _
                                          "',  ThoiGianThue      = '" & tgthue & _
                                           "',  NgayDang      = '" & ngaydang & _
                                             "',  SoNguoiO      = '" & songuoio & _
                                              "',  MaNhanVien      = '" & MaNhanVien & _
                                               "',  CMNDChuNha      = '" & cmndchunha & "' WHERE MaThongTinNha  = '" & manha & "'"

        End If
        Return connect.Execute(sqlstr)
    End Function

    Public Function GetData() As List(Of NhaDTO)
        Dim i As Integer
        Dim manha, maloainha, dientich, giathue, matienich, tgthue, ngaydang, songuoio, MaNhanVien, cmndchunha As String
        Dim nDTO As NhaDTO
        Dim nList As New List(Of NhaDTO)
        Dim dt As New DataTable
        dt = connect.GetData("select * from THONGTINNHA")
        For i = 0 To dt.Rows.Count() - 1
            manha = dt.Rows(i)("MaThongTinNha").ToString
            maloainha = dt.Rows(i)("MaLoaiNha").ToString
            dientich = dt.Rows(i)("DienTich").ToString
            giathue = dt.Rows(i)("GiaThue").ToString
            matienich = dt.Rows(i)("MaTienIch").ToString
            tgthue = dt.Rows(i)("ThoiGianThue").ToString
            ngaydang = dt.Rows(i)("NgayDang").ToString
            songuoio = dt.Rows(i)("SoNguoiO").ToString
            MaNhanVien = dt.Rows(i)("MaNhanVien").ToString
            cmndchunha = dt.Rows(i)("CMNDChuNha").ToString


            nDTO = New NhaDTO(manha, maloainha, dientich, giathue, matienich, tgthue, ngaydang, songuoio, MaNhanVien, cmndchunha)
            nList.Add(nDTO)
        Next
        Return nList
    End Function

    Public Function XoaNha(ByVal emp As NhaDTO)
        Dim manha As String = emp.MaThongTinNha()
        sqlstr = "DELETE THONGTINNHA WHERE MaThongtinNha = '" & manha & "'"
        Return connect.Execute(sqlstr)
    End Function
End Class
